<template>
  <ele-modal
    :width="950"
    :visible="visible"
    :confirm-loading="loading"
    :title="'新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 5 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="24" :sm="24" :xs="24">
          <a-form-item label="供应商名称" v-bind="validateInfos.name"
            ><a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.name"
              autocomplete="off"
          /></a-form-item>
          <a-form-item label="所在地区" v-bind="validateInfos.region">
            <a-cascader
              :fieldNames="fieldNames"
              placeholder="请选择所在地区"
              :options="options"
              v-model:value="defaultValue"
              @change="changeRegion"
            />
          </a-form-item>
          <a-form-item label="详细地址"
            ><a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.address"
              autocomplete="off"
          /></a-form-item>
          <a-form-item label="联系电话"
            ><a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.mobile"
              autocomplete="off"
          /></a-form-item>
          <a-form-item label="联系人"
            ><a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.contact"
              autocomplete="off"
          /></a-form-item>
          <a-form-item label="邮箱"
            ><a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.email"
              autocomplete="off"
          /></a-form-item>
          <a-form-item label="开户银行"
            ><a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.account_bank"
              autocomplete="off"
          /></a-form-item>
          <a-form-item label="银行账号"
            ><a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.account"
              autocomplete="off"
          /></a-form-item>
          <a-form-item label="开户名称"
            ><a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.account_name"
              autocomplete="off"
          /></a-form-item>

          <a-form-item label="备注"
            ><a-textarea
              allow-clear
              placeholder="请输入"
              v-model:value="form.note"
              autocomplete="off"
          /></a-form-item>
          <a-form-item label="排序"
            ><a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.sort"
              autocomplete="off"
          /></a-form-item>
          <a-form-item label="状态"
            ><a-switch v-model:checked="checked" @change="change"
          /></a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { get_address } from '@/api/tenant/store';
  import { add } from '@/api/material/supplier';
  // 表格列配置
  const useForm = Form.useForm;

  const emit = defineEmits(['done', 'update:visible']);

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean
  });

  // 提交状态
  const loading = ref(false);
  const checked = ref(true);
  const options = ref([]);
  const defaultValue = ref(null);

  // 表单数据
  const form = reactive({
    name: '',
    region: '',
    contact: '',
    mobile: '',
    address: '',
    account_bank: '',
    account: '',
    account_name: '',
    email: '',
    note: '',
    sort: '',
    status: 1
  });
  onMounted(() => {
    get_address().then((res) => {
      options.value = res;
    });
  });
  // 地址选择下拉框更改label、balue
  const fieldNames = ref({
    label: 'label',
    value: 'value'
  });
  const changeRegion = (e) => {
    console.log('地区改变：', e);
    form.region = e[e.length - 1];
  };

  const change = (e) => {
    form.status = e ? '1' : '0';
  };

  // 表单验证规则
  const rules = reactive({
    name: [
      {
        required: true,
        message: '请输入供应商名称',
        type: 'string',
        trigger: 'change'
      }
    ],
    region: [
      {
        required: true,
        message: '请选择所在地区',
        type: 'string',
        trigger: 'change'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);

  /* 保存*/
  const save = () => {
    validate()
      .then(() => {
        add(form)
          .then((res) => {
            message.success(res.msg);
            emit('done');
          })
          .catch((e) => {
            message.error(e);
          });
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
